package api

import (
	"api-gatwary/basic/globar"
	__ "api-gatwary/basic/proto"
	"api-gatwary/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Videoadd(c *gin.Context) {
	var req request.VideoReq
	// 开始事务

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	videoadd, err := globar.UserClients.Videoadd(c, &__.VideoaddReq{
		UserId: int64(req.UserId),
		Title:  req.Title,
		Image:  req.Image,
		Info:   req.Info,
		Price:  req.Price,
		Num:    int64(req.Num),
		Types:  int64(req.Types),
		Status: int64(req.Status),
	})
	if err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "添加失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "添加成功",
		"data":    videoadd,
	})
	return
}

func VideoUpdate(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	videoUpdate, err := globar.UserClients.VideoUpdate(c, &__.VideoUpdateReq{
		UserId: int64(req.UserId),
		Title:  req.Title,
		Image:  req.Image,
		Info:   req.Info,
		Price:  req.Price,
		Num:    int64(req.Num),
		Types:  int64(req.Types),
		Status: int64(req.Status),
		Id:     int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "修改失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "修改成功",
		"data":    videoUpdate,
	})
	return
}

func VideoList(c *gin.Context) {
	var req request.VideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "验证失败",
			"data":    false,
		})
		return
	}
	list, err := globar.UserClients.VideoList(c, &__.VideoListReq{})
	if err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{
			"code":    http.StatusUnauthorized,
			"Message": "查看失败",
			"data":    false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"Message": "查看成功",
		"data":    list,
	})
	return
}
